﻿using System.Collections.Generic;
using System.Data;

namespace ncp.Business
{
    public class LogLoginBll
    {
        /// <summary>
        /// 增加登录日志
        /// </summary>
        public static void AddLoginLog(Dictionary<string, object> dicLoginLog, string pDatabase)
        {
            string mySQL = "UPDATE dbo.sa_LogLogin set Log_Status=1 where User_Id='" + dicLoginLog["user_id"].ToString() + "' and Log_Status=0; ";
            mySQL += "INSERT INTO dbo.sa_LogLogin (User_Id, ComputerName, ComputerIP, LoginTime, Log_Status) " +
                     "VALUES ('" + dicLoginLog["user_id"].ToString() + "', '" + dicLoginLog["computername"].ToString() + "', '" + dicLoginLog["computerip"].ToString() + "',getdate(),0)";
            CommonBll.ExecuteTransaction(mySQL, pDatabase);
        }

        /// <summary>
        /// 更新登录日志
        /// </summary>
        public static void UpdateLoginLeave(string User_Id, string pDatabase)
        {
            string mySQL = "UPDATE dbo.sa_LogLogin set Log_Status=1,leaveTime=getdate() where User_Id='" + User_Id + "' and Log_Status=0;";
            CommonBll.ExecuteNonQuery(mySQL, pDatabase);
        }

        /// <summary>
        /// 得到分页登录日志表
        /// </summary>
        public static DataTable GetLoginLogTable(int piPageIndex, int piPageSize, string pcFieldList, string pcOrderBy, string pcWhere, string pDatabase)
        {
            string sqlStr = CommonBll.GetPagingSQL(piPageIndex, piPageSize, "sa_LogLogin_v", pcFieldList, "Log_Id", pcOrderBy, pcWhere);
            return CommonBll.GetdtList(sqlStr, pDatabase);
        }

        /// <summary>
        /// 得到登录日志表
        /// </summary>
        public static DataTable GetLoginLogTable(string pcWhere, string pcOrderBy, string pDatabase)
        {
            string sqlStr = "select * from sa_LogLogin_v ";
            if (!string.IsNullOrEmpty(pcWhere))
                sqlStr += " where " + pcWhere;
            if (!string.IsNullOrEmpty(pcOrderBy))
                sqlStr += " order by " + pcOrderBy;
            return CommonBll.GetdtList(sqlStr, pDatabase);
        }


        //得到记录数
        public static int GetLogLoginCount(string pcWhere, string p_DataBase)
        {
            string sqlStr = "select count(*) from sa_LogLogin ";
            if (!string.IsNullOrEmpty(pcWhere))
                sqlStr += " where " + pcWhere;
            return CommonBll.ReadInteger(sqlStr, p_DataBase);
        }
    }
}
